package servlet.util;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;

public class FileSaveToDsk {
	
	private HttpServletRequest request=null;
	public FileSaveToDsk(HttpServletRequest request) {
		this.request=request;
	}
	private String clientName="";
	public String getClientName() {
		return clientName;
	}
	public void FileSave(FileItem fileItem,String filepath){
		String filename = fileItem.getName();//文件名
		if(!filename.isEmpty()){
			String ext = filename.substring(filename.lastIndexOf("."));
			clientName=System.currentTimeMillis()+ext;
			String virtualPath = filepath+"/"+clientName;
			String realPath = request.getServletContext().getRealPath(virtualPath);
			File file = new File(realPath);
			try {
				fileItem.write(file);
			} catch (Exception e) {
				throw new RuntimeException("文件保存失败",e);
			}
		}
	}
	
}